part of 'widget.dart';

class _LexicalItemImagePickerWloc
    extends StatefulWloc<_LexicalItemImagePickerWlocState, LexicalItemImagePicker> {
  _LexicalItemImagePickerWloc();

  late final CarouselController controller;

  @override
  _LexicalItemImagePickerWlocState get initialState {
    return const _LexicalItemImagePickerWlocState(
      activeImageIndex: 0,
    );
  }

  String getFileNameLabel({
    required String filePath,
  }) {
    final fileName = filePath.split('/').last;
    return fileName.replaceFirst('.jpg', '');
  }

  void onImageChanged({
    required int index,
  }) {
    emit(state.copyWith(
      activeImageIndex: () => index,
    ));
  }

  void onPreviousImageButtonPressed() {
    final previousIndex =
        state.activeImageIndex > 0 ? state.activeImageIndex - 1 : widget.imagePaths.length - 1;

    controller.jumpToPage(previousIndex);
  }

  void onNextImageButtonPressed() {
    final nextIndex =
        state.activeImageIndex < widget.imagePaths.length - 1 ? state.activeImageIndex + 1 : 0;

    controller.jumpToPage(nextIndex);
  }

  void onImagePickingButtonPressed() {
    final pickedImagePath = widget.imagePaths[state.activeImageIndex];
    widget.onImagePicked(pickedImagePath);
  }

  @override
  void initState() {
    super.initState();

    controller = CarouselController();
  }

  @override
  Widget build(BuildContext context) {
    return _LexicalItemImagePickerTheme(
      brightness: CoreTheme.of(context).brightness,
      child: _LexicalItemImagePickerView(
        wloc: this,
      ),
    );
  }
}
